﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text.RegularExpressions;

using DotNetNuke;
using DotNetNuke.Common;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Security;
using DotNetNuke.Services.Exceptions;
using DotNetNuke.Services.Localization;
using DotNetNuke.Services.FileSystem;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Communications;


namespace CharityGamesOnline.Modules.Common
{
    public partial class PagingModule : PortalModuleBase
    {
        private int currentpage;
        private const int PagerCount = 5;
        private int totalpage;
        private int totalcount;
        private int pagesize;
        private int maxpagecount;

        private string EVENTTARGET;
        private string[] ARGS;
        public event System.EventHandler PageChangeEvent;
        public const int DefaultPageSize = 20;
        public const int MAXPAGECOUNT = 100;        

        public int CurrentPage
        {
            get
            {
                if (currentpage == 0)
                {
                    if (ViewState[this.ID + "_Page"] != null)
                        currentpage = Convert.ToInt32(ViewState[this.ID + "_Page"]);
                    else
                        currentpage = 1;
                }
                return currentpage;
            }
            set
            {
                currentpage = value;
                ViewState[this.ID + "_Page"] = value;
            }
        }

        public int TotalPage
        {
            get
            {
                if (totalpage == 0)
                {
                    if ((TotalCount % PageSize) == 0)
                        totalpage = TotalCount / PageSize;
                    else
                        totalpage = (TotalCount / PageSize) + 1;
                }
                return totalpage;
            }
            set
            {
                totalpage = value;
            }
        }

        public int TotalCount
        {
            get
            {
                if (totalcount == 0)
                {
                    if (txtTotalCount.Value.Length != 0)
                        totalcount = Convert.ToInt32(txtTotalCount.Value);
                }
                return totalcount;
            }
            set
            {
                totalcount = value;
                txtTotalCount.Value = totalcount.ToString();
            }
        }

        public int MaxPageCount
        {
            get
            {
                if (maxpagecount == 0)
                    return MAXPAGECOUNT;
                else
                    return maxpagecount;
            }
            set
            {
                maxpagecount = value;
            }
        }

        public int PageSize
        {
            get
            {
                if (pagesize == 0)
                    return DefaultPageSize;
                else
                    return pagesize;
            }
            set { pagesize = value; }
        }

        public bool DirectPageVisible
        {
            set
            {
                DirectPageTD.Visible = value;
            }
        }

        public bool TotalCountVisible
        {
            set
            {
                TotalCountTD.Visible = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            TotalCountVisible = true;                        
        }

        protected override void OnInit(EventArgs e)
        {
            CreateLink(PagerCount);
            base.OnInit(e);            
        }
        
        protected override void OnPreRender(EventArgs e)
        {
            SetControl();
            base.OnPreRender(e);
        }        

        private void SetControl()
        {
            int MaxPage = 0;
            if (MaxPageCount != 0)
                MaxPage = TotalPage > MaxPageCount ? MaxPageCount : TotalPage;
            else
                MaxPage = TotalPage;
            TotalCountLabel.Text = String.Format("{0:N0}", TotalCount);
            TotalPageLabel.Text = MaxPage.ToString();
            CurrentPageLabel.Text = CurrentPage.ToString();            
            
            if (CurrentPage == 1)
            {
                imgBtnFirst.Visible = false;
                imgBtnPre.Visible = false;                
            }
            else
            {
                imgBtnFirst.Visible = true;
                imgBtnPre.Visible = true;
                imgBtnPre.CommandArgument = (CurrentPage - 1).ToString();
            }
            
            imgBtnLast.CommandArgument = MaxPage.ToString();
            if (CurrentPage == MaxPage)
            {
                imgBtnLast.Visible = false;
                imgBtnNext.Visible = false;                
            }
            else
            {
                imgBtnLast.Visible = true;
                imgBtnNext.Visible = true;
                imgBtnNext.CommandArgument = (CurrentPage + 1).ToString();
            }

            int StartPage = 0, EndPage = 0;

            if (MaxPage < PagerCount)
            {
                StartPage = 1;
                EndPage = TotalPage;
            }
            else
            {
                int Half;
                if (PagerCount % 2 == 0)
                    Half = PagerCount / 2;
                else
                    Half = (PagerCount - 1) / 2;
                if (CurrentPage <= Half)
                {
                    StartPage = 1;
                    EndPage = PagerCount;
                }
                else
                {
                    if (CurrentPage + Half > MaxPage)
                        EndPage = MaxPage;
                    else
                        EndPage = CurrentPage + Half;
                    StartPage = EndPage - PagerCount + 1;
                }
            }
            
            UpdateLink(StartPage, EndPage, CurrentPage);
        }
        private void CreateLink(int cntPage)
        {            
            for (int i = 0; i < cntPage; i++)
            {                
                Label BlankLabel = new Label();
                BlankLabel.Text = @"&nbsp;";
                LinkButton tempLinkBtn = new LinkButton();
                tempLinkBtn.ID = "linkBtn_" + i.ToString();
                tempLinkBtn.Click += new EventHandler(navBtnFirst_Click);                
                PageNumTD.Controls.Add(tempLinkBtn);
                PageNumTD.Controls.Add(BlankLabel);
            }
        }
        private void UpdateLink(int Start, int End, int Current)
        {
            //Initilize link button status
            LinkButton[] linkBtnCandidates = new LinkButton [PageNumTD.Controls.Count];
            int linkBtnIndex = 0;
            foreach (Control ctrl in PageNumTD.Controls)
            {                
                LinkButton btnLink = ctrl as LinkButton;
                if (btnLink != null)
                {
                    btnLink.Text = string.Empty;
                    btnLink.CommandArgument = string.Empty;
                    btnLink.Visible = false;
                    btnLink.Enabled = true;
                    linkBtnCandidates[linkBtnIndex] = btnLink;
                    linkBtnIndex++;
                }
            }
            //bind command arg and text to link button
            linkBtnIndex = 0;
            for (int i = Start; i <= End; i++)
            {
                LinkButton tempLinkBtn = linkBtnCandidates[linkBtnIndex];
                tempLinkBtn.Text = i.ToString();
                tempLinkBtn.CommandArgument = i.ToString();
                tempLinkBtn.Visible = true;
                if (i == Current)
                        tempLinkBtn.Enabled = false;
                linkBtnIndex++;
            }
        }        
        
        protected void JumpPage_TextChanged(object sender, EventArgs e)
        {
            if (JumpPage.Text.Length != 0)
            {
                int Current = 0;
                Regex r = new Regex(@"^\d+$");
                if (r.IsMatch(JumpPage.Text))
                {
                    int MaxPage = 0;
                    if (MaxPageCount != 0)
                        MaxPage = TotalPage > MaxPageCount ? MaxPageCount : TotalPage;
                    else
                        MaxPage = TotalPage;
                    Current = Convert.ToInt32(JumpPage.Text);
                    if (Current > 0 && Current <= MaxPage)
                        CurrentPage = Current;
                    else
                    {
                        if (Current <= 0)
                            CurrentPage = 1;
                        else
                            CurrentPage = MaxPage;
                    }
                    if (PageChangeEvent != null)
                        PageChangeEvent(this, EventArgs.Empty);
                    JumpPage.Text = "";
                }
            }
        }

        protected void navBtnFirst_Click(object sender, EventArgs e)
        {
            CurrentPage = Int32.Parse((sender as IButtonControl).CommandArgument);
            if (PageChangeEvent != null)
                PageChangeEvent(this, EventArgs.Empty);
        }
    }
}